package com.manager.util;

import java.util.List;

import org.displaytag.pagination.PaginatedList;
import org.displaytag.properties.SortOrderEnum;

/*
 * displaytag分页列表
 *  @author  collonn
 */
public class PageList implements PaginatedList {
	/*
	 * 每页列表，即每页显示的记录
	 */
	private List list;

	/*
	 * 当前页码，即当前第几页
	 */
	private int pageNumber = 1;

	/*
	 * 每页记录数 pagesize
	 */
	private int objectsPerPage = 15;

	/*
	 * 总记录数
	 */
	private int fullListSize = 0;
	
	private String sortCriterion;

	private SortOrderEnum sortDirection;

	private String searchId;

	public List getList() {
		return list;
	}

	public void setList(List list) {
		this.list = list;
	}

	public int getPageNumber() {
		return pageNumber;
	}

	public void setPageNumber(int pageNumber) {
		this.pageNumber = pageNumber;
	}

	public int getObjectsPerPage() {
		return objectsPerPage;
	}

	public void setObjectsPerPage(int objectsPerPage) {
		this.objectsPerPage = objectsPerPage;
	}

	public int getFullListSize() {
		return fullListSize;
	}

	public void setFullListSize(int fullListSize) {
		this.fullListSize = fullListSize;
	}

	public String getSortCriterion() {
		return sortCriterion;
	}

	public void setSortCriterion(String sortCriterion) {
		this.sortCriterion = sortCriterion;
	}

	public SortOrderEnum getSortDirection() {
		return sortDirection;
	}

	public void setSortDirection(SortOrderEnum sortDirection) {
		this.sortDirection = sortDirection;
	}

	public String getSearchId() {
		return searchId;
	}

	public void setSearchId(String searchId) {
		this.searchId = searchId;
	}
	
	/*
	 * 总页数
	 */
	public int getTotalPage() {
		int t = this.fullListSize/this.objectsPerPage;
		return this.fullListSize%this.objectsPerPage ==0 ? t : t + 1;
	}
	
	/*
	 * 当前第n-m条记录
	 */
	public String getCurrentListNum() {
		int startNum = this.objectsPerPage*(this.pageNumber - 1) + 1;
		int endNum = 0;
		if(this.pageNumber == this.getTotalPage()){
			endNum = this.fullListSize;
		}else{
			endNum = startNum + this.objectsPerPage - 1;
		}
		
		return "[" + startNum + "-" + endNum + "]";
	}

	/*
	 * 如果是mysql，由于mysql用selet xx from xx order by xx limit getOffset(),objectsPerPage
	 * mysql是从0开始的
	 */
	public int getOffset() {
		return (this.getPageNumber() - 1) * this.getObjectsPerPage();
	}

	/*
	 * 如果是oracle，由于oracle用select * from where rownum between getOffsetStart() and getOffsetEnd()
	 * oracle是从1开始的
	 */
	public int getOffsetStart() {
		return (this.getPageNumber() - 1) * this.getObjectsPerPage() + 1;
	}

	public int getOffsetEnd() {
		return this.getOffsetStart() + this.objectsPerPage - 1;
	}

}